<?php

namespace app\supermanager\controller;

use think\Controller;
use think\Request;
use \think\Exception;
use app\common\traits\controller\TraitController;

class Setting extends Controller
{
    use TraitController;

    protected $model = [];

    /**
     * 初始化操作
     */
    protected function init()
    {
        if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
        $this->model['settings'] = model('common/Settings');
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        try {
            // $setting_info['short_name'] = 'systems';
            // $setting_info['name']       = '人民币/旅行币比率';
            // $setting_info['profiles']   = ['rate' => $this->request->get('rate', 1)];

            // $this->model['settings']->createSettings($setting_info);
            return $this->fetch('index', ['settings' => $this->model['settings']->all()]);            
        } catch (\Exception $e) {
            
        }
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch('create', ['configs' => $this->model['settings']->getConfigs()]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        try {
            $setting_info['short_name'] = $request->param('type');
            $setting_info['name']       = $request->param('title');
            $setting_info['profiles']   = $request->param('profiles');

            $this->model['settings']->createSettings($setting_info);
            
        } catch (\Exception $e) {
            
        }
        
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        try {
            return $this->fetch('read', ['setting' => $this->model['settings']->get($id)]);            
        } catch (\Exception $e) {

        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        try {
            return $this->fetch('edit', ['setting' => $this->model['settings']->get(['short_name'=>'systems'])]);            
        } catch (\Exception $e) {
            
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
    	if (!is_numeric($this->request->post('profiles')) || $this->request->post('profiles') < 0) {
            $this->error('填写格式错误或不能能为空');
            return false;
        }
        try {
            $setting_info['profiles']   = ['rate' => $this->request->post('profiles')];
            $this->model['settings']->updateSettings($setting_info, ['short_name' => 'systems']);         
        } catch (\Exception $e) {
            $this->error('修改失败');
			return false;
        }
		$this->success('修改成功');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $this->model['settings']->deleteSettings(['id' => $id]);
        } catch (\Exception $e) {
            
        }
    }
}
